package com.csair.seam.domain.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.csair.seam.domain.model.entity.IdleReceiveExecl;
import com.csair.seam.domain.service.IEquipOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csair.seam.domain.mapper.IdleReceiveMapper;
import com.csair.seam.domain.model.entity.IdleReceive;
import com.csair.seam.domain.model.param.IdleReceiveParam;
import com.csair.seam.domain.service.IdleReceiveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class IdleReceiveSerivceImpl implements IdleReceiveService{

	@Autowired
	private IdleReceiveMapper idleReceiveMapper;

	@Autowired
	private IEquipOrgService iEquipOrgService;



	/**
	 * 分页条件查询
	 * @param param
	 * @return
	 */
	@DS("aip")
	@Override
	public List<IdleReceiveExecl> getIdleReceive(IdleReceiveParam param) {

		QueryWrapper<IdleReceive> queryWrapper = new QueryWrapper<>();

		//设备类型
		if(StringUtils.isNotEmpty(param.getCategory())){
			queryWrapper.eq("category",param.getCategory());
		}
		//接收类型
		if(StringUtils.isNotEmpty(param.getIntype())){
			queryWrapper.eq("intype",param.getIntype());
		}
		//原公司
		if(StringUtils.isNotEmpty(param.getOutcorp())){
			queryWrapper.eq("outcorp",param.getOutcorp());
		}
		//接收部门
		if(StringUtils.isNotEmpty(param.getIndept())){
			queryWrapper.eq("indept",param.getIndept());
		}
		//接收公司
		if(StringUtils.isNotEmpty(param.getIncorp())){
			queryWrapper.eq("incorp",param.getIncorp());
		}
		if(StringUtils.isNotEmpty(param.getStartDealDate()) && StringUtils.isNotEmpty(param.getEndDealDate())){
			queryWrapper.apply("deal_date BETWEEN  DATE_FORMAT(DATE_ADD({0},INTERVAL -1 day), '%Y-%m-%d' ) " +
					"and DATE_FORMAT(DATE_ADD({1},INTERVAL 1 day), '%Y-%m-%d' )  ",new Object[]{param.getStartDealDate(),param.getEndDealDate()});
		}
		queryWrapper.orderByDesc("deal_date");
		List<IdleReceive> irs = idleReceiveMapper.selectList(queryWrapper);
		List<IdleReceiveExecl> ires = new ArrayList<>();
		irs.stream().forEach(e->{
			IdleReceiveExecl ire = new IdleReceiveExecl(e);
			ires.add(ire);
		});
		return  ires;
	}

	@DS("aip")
	@Override
	public PageInfo<IdleReceive> getIdleReceivePage(IdleReceiveParam param) {

		QueryWrapper<IdleReceive> queryWrapper = new QueryWrapper<>();

		//设备类型
		if(StringUtils.isNotEmpty(param.getCategory())){
			queryWrapper.eq("category",param.getCategory());
		}
		//处置方式
		if(StringUtils.isNotEmpty(param.getIntype())){
			queryWrapper.eq("intype",param.getIntype());
		}
		//原公司
		if(StringUtils.isNotEmpty(param.getOutcorp())){
			queryWrapper.eq("outcorp",param.getOutcorp());
		}
		//接收部门
		if(StringUtils.isNotEmpty(param.getIndept())){
			queryWrapper.eq("indept",param.getIndept());
		}
		//接收公司
		if(StringUtils.isNotEmpty(param.getIncorp())){
			queryWrapper.eq("incorp",param.getIncorp());
		}
		if(StringUtils.isNotEmpty(param.getStartDealDate()) && StringUtils.isNotEmpty(param.getEndDealDate())){
			queryWrapper.apply("deal_date BETWEEN  DATE_FORMAT(DATE_ADD({0},INTERVAL -1 day), '%Y-%m-%d' ) " +
					"and DATE_FORMAT(DATE_ADD({1},INTERVAL 1 day), '%Y-%m-%d' )  ",new Object[]{param.getStartDealDate(),param.getEndDealDate()});
		}
		queryWrapper.orderByDesc("deal_date");
		PageHelper.startPage((int) param.getPageIndex(),param.getPageSize());
		List<IdleReceive> irs = idleReceiveMapper.selectList(queryWrapper);
		irs.stream().forEach(e->{
			e.setPhonenumber(null);
			e.setId(null);
			e.setOperator(null);
			e.setPhonenumber(null);
			e.setRentLen(null);
			e.setConfirman(null);
			e.setEquipcodes(null);
			e.setEquipBrand(null);
			e.setEquipModel(null);
			e.setUsedyear(null);
			e.setCreator(null);
			e.setCreaTime(null);
			e.setShowId(null);
		});
		return  new PageInfo<>(irs);
	}

	/**
	 * 获取年度成交金额
	 */
	@DS("aip")
	@Override
	public BigDecimal getYearInsum() {
		return idleReceiveMapper.getYearInsum() == null ? new BigDecimal(0):idleReceiveMapper.getYearInsum();
	}
	/**
	 * 获取年度成交量
	 */
	@DS("aip")
	@Override
	public Integer getYearReceivnum() {
		return idleReceiveMapper.getYearReceivnum();
	}

	/**
	 * 获取累计成交额度
	 * @return
	 */
	@DS("aip")
	@Override
	public BigDecimal getAllInsum() {
		return idleReceiveMapper.getAllInsum() == null ? new BigDecimal(0):idleReceiveMapper.getAllInsum();
	}

	/**
	 * 获取累计成交数量
	 * @return
	 */
	@DS("aip")
	@Override
	public Integer getAllReceivnum() {
		return idleReceiveMapper.getAllReceivnum();
	}

	/**
	 * 查询发布量
	 * @param startDate 开始时间
	 * @param endDate   结束时间
	 * @param intype    接收类型
	 * @param category  类型大类
	 * @return
	 */
	@DS("aip")
	@Override
	public List<IdleReceive> getOutcorpCountAndInsum(String startDate,
													 String endDate,
													 String intype,
													 String category) {
		List<IdleReceive> ids = idleReceiveMapper.getAllOutcorp();
		List<IdleReceive> irs  = idleReceiveMapper.getOutcorpCountAndInsum(startDate,endDate, intype, category);
		compare(irs,ids,true);
		return irs;
	}

	/**
	 * 查询接收量
	 * @param startDate 开始时间
	 * @param endDate   结束时间
	 * @param intype    接收类型
	 * @param category  类型大类
	 * @return
	 */
	@DS("aip")
	@Override
	public List<IdleReceive> getIncorpCountAndInsum(String startDate,
													String endDate,
													String intype,
													String category) {
		//List<IdleReceive> ids = idleReceiveMapper.getAllIncorp();
		List<IdleReceive> irs  = idleReceiveMapper.getIncorpCountAndInsum(startDate,endDate, intype, category);
		//compare(irs,ids,false);
		return irs;
	}

	/**
	 * 批量插入
	 * @param ids
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@DS("aip")
	@Override
	public void addBatchIdleReceive(List<IdleReceive> ids) {
		for(IdleReceive id : ids){
			idleReceiveMapper.insert(id);
		}
	}

	/**
	 * 检查是否有值
	 * @param ir
	 * @param error
	 * @return
	 */
	@DS("aip")
	@Override
	public Map<String, String> checkIdle(IdleReceive ir, Map<String,String> error) {
		if (StringUtils.isEmpty(idleReceiveMapper.getNameByCode(ir.getOutcorp()))){
			error.put(ir.getOutcorp(), "查询不到公司");
		}
		if (StringUtils.isEmpty(idleReceiveMapper.getNameByCode(ir.getIncorp()))){
			error.put(ir.getIndept(), "查询不到公司");
		}
		if(StringUtils.isEmpty(iEquipOrgService.findOrgNameByDeptCode(ir.getIncorp(),ir.getIndept()))){
			error.put(ir.getIndept(), "查询不到公司部门");
		}
		if(StringUtils.isEmpty(iEquipOrgService.findOrgNameByDeptCode(ir.getOutcorp(),ir.getOutdept()))){
			error.put(ir.getOutcorp(), "查询不到公司部门");
		}
		return error;
	}
	@Override
	public String getNameByCode(String code){
		return idleReceiveMapper.getNameByCode(code);
	}
	private List<IdleReceive> compare(List<IdleReceive> irs,List<IdleReceive> removeIrs,boolean compare){
		if(irs.isEmpty()){
			irs.addAll(removeIrs);
			return irs;
		}
		if(removeIrs.isEmpty()){
			return irs;
		}
		Iterator<IdleReceive> itr = removeIrs.iterator();
		while (itr.hasNext()){
			IdleReceive rid = itr.next();
			irs.stream().forEach(e->{
				if(StringUtils.equals(rid.getOutcorp(),e.getOutcorp())&&compare){
					itr.remove();
				}else if(StringUtils.equals(rid.getIncorp(),e.getIncorp())&&!compare){
					itr.remove();
				}
			});
		}
		irs.addAll(removeIrs);
		return irs;
	}
}
